﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using SLRTHackathon.DataModels;
using SLRTHackathon.ViewModels;
using Microsoft.Phone.Tasks;

namespace SLRTHackathon.Views
{
    public partial class CarHelperPage : PhoneApplicationPage
    {
        private static Car _currentCar;

        public static Car CurrentCar
        {
            get { return _currentCar; }
            set { _currentCar = value; }
        }

        public CarHelperPage()
        {
            InitializeComponent();
            App.ViewModel.LoadDependenciesForCar(CurrentCar);
            
            //Load federal taxes
            this.BrandCarTextBlock.Text = _currentCar.Brand;
            this.ModelCarTextBlock.Text = _currentCar.Model;
            this.CIDCarTextBlock.Text = _currentCar.CID.ToString();
            this.TaxCarTextBlock.Text = ((_currentCar.CID * 8) / 200).ToString();

            
            //Load insurances
            InsurenceStackPanel.Children.Clear();
            foreach (Insurance insurance in App.ViewModel.InsuranceForCar) 
            {
                StackPanel sp = new StackPanel();
                sp.Orientation = System.Windows.Controls.Orientation.Horizontal;
                TextBlock tb1 = new TextBlock();
                tb1.Text = "Name:";
                TextBlock tb2 = new TextBlock();
                tb2.Text = insurance.Name;
                StackPanel sp2 = new StackPanel();
                sp2.Orientation = System.Windows.Controls.Orientation.Horizontal;
                TextBlock tb3 = new TextBlock();
                tb3.Text = "Description:";
                TextBlock tb4 = new TextBlock();
                tb4.Text = insurance.Description;
                sp.Children.Add(tb1);
                sp.Children.Add(tb2);
                sp2.Children.Add(tb3);
                sp2.Children.Add(tb4);
                InsurenceStackPanel.Children.Add(sp);
                InsurenceStackPanel.Children.Add(sp2);
            }

            //Load extended insurances
            
            ExtendedInsurenceStackPanel.Children.Clear();
            foreach (ExtendedInsurance insurance in App.ViewModel.ExtendedInsuranceForCar)
            {
                StackPanel sp = new StackPanel();
                sp.Orientation = System.Windows.Controls.Orientation.Horizontal;
                TextBlock tb1 = new TextBlock();
                tb1.Text = "Name:";
                TextBlock tb2 = new TextBlock();
                tb2.Text = insurance.Name;
                StackPanel sp2 = new StackPanel();
                sp2.Orientation = System.Windows.Controls.Orientation.Horizontal;
                TextBlock tb3 = new TextBlock();
                tb3.Text = "Description:";
                TextBlock tb4 = new TextBlock();
                tb4.Text = insurance.Description;
                sp.Children.Add(tb1);
                sp.Children.Add(tb2);
                sp2.Children.Add(tb3);
                sp2.Children.Add(tb4);
                ExtendedInsurenceStackPanel.Children.Add(sp);
                ExtendedInsurenceStackPanel.Children.Add(sp2);
            }

            //Load extended insurances

            CustomStackPanel.Children.Clear();
            foreach (CustomDependency insurance in App.ViewModel.CustomDependencyForCar)
            {
                StackPanel sp = new StackPanel();
                sp.Orientation = System.Windows.Controls.Orientation.Horizontal;
                TextBlock tb1 = new TextBlock();
                tb1.Text = "Name:";
                TextBlock tb2 = new TextBlock();
                tb2.Text = insurance.Name;
                StackPanel sp2 = new StackPanel();
                sp2.Orientation = System.Windows.Controls.Orientation.Horizontal;
                TextBlock tb3 = new TextBlock();
                tb3.Text = "Description:";
                TextBlock tb4 = new TextBlock();
                tb4.Text = insurance.Description;
                sp.Children.Add(tb1);
                sp.Children.Add(tb2);
                sp2.Children.Add(tb3);
                sp2.Children.Add(tb4);
                CustomStackPanel.Children.Add(sp);
                CustomStackPanel.Children.Add(sp2);
            }

            //Load inter vign

            VignetteStackPanel.Children.Clear();
            foreach (InternationalVignette insurance in App.ViewModel.InternationalVignetteForCar)
            {
                StackPanel sp = new StackPanel();
                sp.Orientation = System.Windows.Controls.Orientation.Horizontal;
                TextBlock tb1 = new TextBlock();
                tb1.Text = "Country Code:";
                TextBlock tb2 = new TextBlock();
                tb2.Text = insurance.CountryCode;
                StackPanel sp2 = new StackPanel();
                sp2.Orientation = System.Windows.Controls.Orientation.Horizontal;
                TextBlock tb3 = new TextBlock();
                tb3.Text = "Nr of days:";
                TextBlock tb4 = new TextBlock();
                tb4.Text = insurance.NumberOfDaysBeforeEndDate.ToString();
                sp.Children.Add(tb1);
                sp.Children.Add(tb2);
                sp2.Children.Add(tb3);
                sp2.Children.Add(tb4);
                VignetteStackPanel.Children.Add(sp);
                VignetteStackPanel.Children.Add(sp2);
            }
        }

        private void GoToAddInsurenceButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/AddInsurencePage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void GoToAddExtendedButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/AddExtendedInsurancePage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void GoToAddVignetteButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/AddInternationalVignettePage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void GoOnlineVignetteButton_Click(object sender, RoutedEventArgs e)
        {
            WebBrowserTask webBrowserTask = new WebBrowserTask();
            webBrowserTask.Uri = new Uri("http://www.e-rovinieta.ro",UriKind.Absolute);
            webBrowserTask.Show();
        }

        private void GoToAddCustomButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/AddCustomDependencyPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void ListBoxItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            CarHelperPanorama.DefaultItem = CarHelperPanorama.Items[1];
            NavigationService.Navigate(new Uri("/Views/CarHelperPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void ListBoxItem_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
        {
            CarHelperPanorama.DefaultItem = CarHelperPanorama.Items[2];
            NavigationService.Navigate(new Uri("/Views/CarHelperPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void ListBoxItem_Tap_2(object sender, System.Windows.Input.GestureEventArgs e)
        {
            CarHelperPanorama.DefaultItem = CarHelperPanorama.Items[3];
            NavigationService.Navigate(new Uri("/Views/CarHelperPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void ListBoxItem_Tap_3(object sender, System.Windows.Input.GestureEventArgs e)
        {
            CarHelperPanorama.DefaultItem = CarHelperPanorama.Items[4];
            NavigationService.Navigate(new Uri("/Views/CarHelperPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void ListBoxItem_Tap_4(object sender, System.Windows.Input.GestureEventArgs e)
        {
            CarHelperPanorama.DefaultItem = CarHelperPanorama.Items[5];
            NavigationService.Navigate(new Uri("/Views/CarHelperPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void ListBoxItem_Tap_5(object sender, System.Windows.Input.GestureEventArgs e)
        {
            CarHelperPanorama.DefaultItem = CarHelperPanorama.Items[6];
            NavigationService.Navigate(new Uri("/Views/CarHelperPage.xaml", UriKind.RelativeOrAbsolute));
        }

    }
}